// pages/my/my.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nickName: '点击登录',
    },
    mylogin: function () {



        var that = this;
        wx.getSetting({ //调用接口判断是否登录
            success: res => { //成功调用获取信息API
                console.log('调用getsetting成功');
                wx.getUserInfo({
                    success: res => { //调用成功获取用户信息
                        console.log('获取信息成功' + res);
                        this.setData({ //将用户信息传递给data
                            avatarUrl: res.userInfo.avatarUrl,
                            nickName: res.userInfo.nickName,
                        })
                        wx.cloud.callFunction({ //调用云函数，获取用户openid
                            name: 'login',
                            data: {},
                            success: res => { //调用成功
                                console.log('云函数调用成功')
                            },
                            fail: err => {
                                console.error(' 调用云函数失败', err)
                            }
                        })

                    }
                })
            },
            fail: res => {
                console.log('接口调用失败')
            }
        })
    },
    getPhoneNumber(e) {
        var that = this;
        wx.cloud.callFunction({
            name: 'getMobile',
            data: {
                weRunData: wx.cloud.CloudID(e.detail.cloudID),
            }
        }).then(res => {
            that.setData({
                mobile: res.result,
            })
            console.log(res.result)
            wx.setStorageSync('phone', res.result)
            wx.request({ //发送请求
                url: 'https://zg99.offcn.com/index/chaxun/register?actid=26067&callback=?', //接收url
                data: {
                    phone: res.result,
                    fenxiao: this.data.area,
                    geneal: this.data.geneal
                }, //接收参数
                success(res) {
                    var jsonStr = res.data;
                    jsonStr = jsonStr.replace("(", "");
                    jsonStr = jsonStr.replace(")", "");
                    if (typeof jsonStr != 'object') {
                        jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                        var theJson = JSON.parse(jsonStr);
                    }
                    let constscene = wx.getStorageSync('scene')
                    if (constscene) {
                        let phone = wx.getStorageSync('phone')
                        // 记录加一 tianxu 10-11
                        wx.request({
                            url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/get_we_chat_scene_code',
                            method: 'POST',
                            data: {
                                scene: constscene,
                                mobile: phone
                            },
                            success: res => {
                                console.log(res);
                            }
                        })
                    }



                    console.log(theJson)
                }
            })
        }).catch(err => {
            console.error(err);
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        console.log(options);
        that.setData({
            area: options.area,
            geneal: options.geneal
        })


        var mobile = wx.getStorageSync('phone')
        that.setData({
            mobile: mobile
        })

        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.userInfo']) {
                    wx.getUserInfo({
                        success: res => {
                            this.setData({
                                avatarUrl: res.userInfo.avatarUrl,
                                nickName: res.userInfo.nickName,
                            })
                        }
                    })
                }
            }
        })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /************************************
     * 页面相关事件处理函数--监听用户下拉动作 *
     ************************************/
    onPullDownRefresh: function () {

    },

    /************************
     * 页面上拉触底事件处理函数 *
     ************************/
    onReachBottom: function () {

    },
    tozhiwei: function () {
        wx.navigateToMiniProgram({
            appId: 'wx3bfce5135e23ad3f', //国考职位查询小程序appid
            path: 'pages/index/index', //国考职位查询首页index
            envVersion: 'release', //develop（开发版），trial（体验版），release（正式版） 
            success(res) {
                console.log('打开成功！')
            }
        })
    },
    tosearch: function () {
        wx.navigateTo({
            url: '../search/search?area=' + this.data.area + '&geneal=' + this.data.geneal
        })
    },
    toquestion: function () {
        wx.navigateTo({
            url: '../question/question?area=' + this.data.area + '&geneal=' + this.data.geneal
        })
    },
    togkbs: function () {
        wx.navigateTo({
            url: '../gkbs/gkbs'
        })
    },
    tomokao: function () {
        wx.navigateToMiniProgram({
            appId: 'wx764acafb2207d2a2', //模考appid
            path: 'pages/welcome/welcome', //模考
            envVersion: 'release', //develop（开发版），trial（体验版），release（正式版） 
            success(res) {
                console.log('打开成功！')
            }
        })
    },
    tozx: function () {
        wx.navigateTo({
            url: '../xiaoneng/xiaoneng'
        })
    },
    torili: function () {
        var value = wx.getStorageSync('phone')
        if (value) {
            wx.navigateTo({
                url: '../calendar/calendar?area=' + this.data.area + '&geneal=' + this.data.geneal
            })
        } else {
            wx.showToast({
                title: "请先登陆+绑定手机",
                icon: 'none',
                duration: 2000,
                success: function () {
                    setTimeout(function () {
                        console.log('1')
                        wx.switchTab({
                            url: '../my/my'
                        })
                    }, 2000);
                }
            });

        }

    },
    to19: function () {
        wx.navigateToMiniProgram({
            appId: 'wxca86930ec3e80717', //国考职位查询小程序appid
            path: 'page/course/details?fenxiang=1&id=187598', //国考职位查询首页index
            envVersion: 'release', //develop（开发版），trial（体验版），release（正式版） 
            success(res) {
                console.log('打开成功！')
            }
        })
    },

    /*******************
     * 用户点击右上角分享 *
     *******************/
    onShareAppMessage: (res) => {
        if (res.from === 'button') {
            console.log("来自页面内转发按钮");
            console.log(res.target);
        } else {
            console.log("来自右上角转发菜单")
        }
        return {
            title: '2021国家公务员智能职位检索系统',
            path: '/pages/index/index',
            imageUrl: "http://sd.offcn.com/zg/wximg/gkhz-img/banner_1.jpg",
            success: (res) => {
                console.log("转发成功", res);
            },
            fail: (res) => {
                console.log("转发失败", res);
            }
        }
    }
})
